//logs.js
const titleOptions = require('../../utils/title.js')
import api from "../../api/cloudApi.js"

Page({
  data: {
    dialogAnimate: false,
    titles: {

      "history": {
        label: '历史',
        score: '',
        title: '',
        ratio: ''
      },
      "physical": {
        label: '物理',
        score: '',
        title: '',
        ratio: ''
      },
      "chemistry": {
        label: '化学',
        score: '',
        title: '',
        ratio: ''
      },
      "politics": {
        label: '政治',
        score: '',
        title: '',
        ratio: ''
      },
      "maths": {
        label: '数学',
        score: '',
        title: '',
        ratio: ''
      },
      "chinese": {
        label: '语文',
        score: '',
        title: '',
        ratio: ''
      },
      "english": {
        label: '英语',
        score: '',
        title: '',
        ratio: ''
      },
    },
    newTitle: [],
    showingTitleIndex: 0,
    showNext: true
  },

  onLoad: function() {
    this.getCloudScore()
  },

  async getCloudScore() {
    let res = await api.getScores()
    let courses = ["maths", "chinese", "english", "chemistry", "politics", "physical", "history"]
    courses.forEach((c, index) => {
      let score = Math.max.apply(null, res.map(t => t[c]))
      let max_score = index < 3 ? 160 : 130
      let ratio = ~~(score * 100 / max_score)
      let title = titleOptions
      let prefix = 'titles.' + c
      this.setData({
        [prefix + '.score']: score,
        [prefix + '.ratio']: ratio,
        [prefix + '.title']: this.getTitle(ratio),
      })
    })

    this.diffOldTitle(this.data.titles)
  },

  getTitle(ratio) {
    let scoreList = Object.keys(titleOptions).sort((a, b) => a - b)
    let targetScore = scoreList.find(t => ratio < t)
    return titleOptions[targetScore]
  },

  diffOldTitle(newTitle) {
    let _this = this;
    wx.getStorage({
      key: 'titles',
      complete: function() {
        _this.saveTitles()
        if (_this.data.newTitle.length) {
          _this.setData({
            dialogAnimate: true
          })
        }
      },
      fail: function(err) {
        let tempArr = []
        for (let key in newTitle) {
          tempArr.push(newTitle[key])
        }
        _this.setData({
          newTitle: tempArr
        })
      },
      success: function({
        data
      }) {
        let tempArr = []
        for (let key in data) {
          let isDiff = newTitle[key].title.title != data[key].title.title
          if (isDiff) {
            tempArr.push(newTitle[key])
          }
        }
        _this.setData({
          newTitle: tempArr
        })
      },
    })
  },

  saveTitles() {
    wx.setStorage({
      key: 'titles',
      data: this.data.titles
    })
    console.log(this.data.newTitle)
  },
  nextTitle() {
    this.setData({
      dialogAnimate: false
    })
    if (!this.data.showNext || this.data.showingTitleIndex === this.data.newTitle.length - 1) {
      this.setData({
        showingTitleIndex: 0,
        newTitle: []
      })
      return;
    }
    this.setData({
      showingTitleIndex: this.data.showingTitleIndex + 1
    })
    this.setData({
      dialogAnimate: true
    })
  },

  showDetial(ev) {
    let key = ev.currentTarget.dataset.key
    this.setData({
      newTitle: [this.data.titles[key]],
      showNext: false
    })
    this.setData({
      dialogAnimate: true
    })
  }
})